package org.dromara.wyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.wyl.domain.NppDatasetPrediction;
import org.dromara.wyl.domain.bo.NppDatasetPredictionBo;
import org.dromara.wyl.domain.vo.NppDatasetPredictionVo;
import org.dromara.wyl.mapper.NppDatasetPredictionMapper;
import org.dromara.wyl.service.INppDatasetPredictionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class NppDatasetPredictionServiceImpl implements INppDatasetPredictionService {

    private final NppDatasetPredictionMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public NppDatasetPredictionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<NppDatasetPredictionVo> queryPageList(NppDatasetPredictionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NppDatasetPrediction> lqw = buildQueryWrapper(bo);
        Page<NppDatasetPredictionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<NppDatasetPredictionVo> queryList(NppDatasetPredictionBo bo) {
        LambdaQueryWrapper<NppDatasetPrediction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NppDatasetPrediction> buildQueryWrapper(NppDatasetPredictionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NppDatasetPrediction> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSimulatorPower()), NppDatasetPrediction::getSimulatorPower, bo.getSimulatorPower());
        lqw.eq(StringUtils.isNotBlank(bo.getCircuitMeasurement()), NppDatasetPrediction::getCircuitMeasurement, bo.getCircuitMeasurement());
        lqw.eq(bo.getImpMeasurement() != null, NppDatasetPrediction::getImpMeasurement, bo.getImpMeasurement());
        lqw.eq(StringUtils.isNotBlank(bo.getDatasetType()), NppDatasetPrediction::getDatasetType, bo.getDatasetType());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), NppDatasetPrediction::getState, bo.getState());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(NppDatasetPredictionBo bo) {
        NppDatasetPrediction add = MapstructUtils.convert(bo, NppDatasetPrediction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(NppDatasetPredictionBo bo) {
        NppDatasetPrediction update = MapstructUtils.convert(bo, NppDatasetPrediction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NppDatasetPrediction entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(NppDatasetPrediction modelLsc) {
        return baseMapper.update(
            new LambdaUpdateWrapper<NppDatasetPrediction>()
                .eq(NppDatasetPrediction::getId, modelLsc.getId())
                .set(NppDatasetPrediction::getState, modelLsc.getState())
        );
    }
}
